/*
 * =====================================================================================
 *
 *       Filename:  InetAddr.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  07/11/2012 09:44:56 PM
 *       Revision:  none
 *       Compiler:  gcc/g++
 *
 *         Author:  wangtengchao , wangtengchao AT gmail.com
 *   Organization:  
 *
 * =====================================================================================
 */

#include"inetAddr.h"
#include"ipString.h" //for class IpString
//#include "netHelp.h" //for ntoh  hton
#include <cstring> //for memset
#include <sys/socket.h>//for PF_INET
using namespace inm::net;



InetAddr::InetAddr(const uint16_t& port)
{
	_setSockAddrIn(INADDR_ANY,port);
}
InetAddr::InetAddr(const IpString& ip,const uint16_t& port)
{//convert IpString to addr_in .then fill the  struct sockaddr_in
    _setSockAddrIn(ip.toInAddr(),port);
}
const struct sockaddr_in& InetAddr::getSockAddr() const
{
    return addr_;
}
const struct in_addr& InetAddr::getInAddr() const
{
    return addr_.sin_addr;
}
in_addr_t InetAddr::getInAddrT()  const
{
    return addr_.sin_addr.s_addr;
    //use in  InetNumericToPresentation(newClientAddr.getInAddrT())
    //InetNumericToPresentation  should use net bytes and pass in addr_sin_addr_.s_addr immediately
    //constrast with getPort below
}
 uint16_t InetAddr::getPort() const
{

    return ntohs(addr_.sin_port);//must use ntohs
}

void InetAddr::setSockAddr(const sockaddr_in &sockAddr)
{//
    addr_ = sockAddr;
}

void InetAddr::_setSockAddrIn(const uint32_t& ip,const uint16_t& port)
{	
	memset(&addr_,0,sizeof(addr_));	
#ifdef linux
    addr_.sin_family = AF_INET;
#else
    addr_.sin_family = PF_INET;
#endif

    addr_.sin_addr.s_addr = htonl(ip);
    addr_.sin_port = htons(port);
}
